var  app = getApp();
let keys = '4UTBZ-PUWLX-UGS4R-TIK3C-WFL73-N4FWH';
//const tabService = require("../../libs/tabService");
const QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
const qqmapsdk = new QQMapWX({
  key:keys
});
Page({ 
    /**
     * 页面的初始数据
     */
    data: {        
        autoplay: true,
        typeValue: null,
        showRightToast: false,
        changeImg: false,
        show: true,
        
        played: false,
        //滑动触点开始的时候
        startPoint: 0,
        currentImageIndex: 0,
        swiperH:'',//swiper高度
         nowIdx:0,//当前swiper索引
         rent:0,
         city_hidden:false,
         status:0
 
    },
 
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      let that=this;
 
    var userInfo=wx.getStorageSync('userInfo');   
  
  
       
      that.setData({
        platform:app.globalData.platform,      
         width:app.globalData.windowWidth,
        height:app.globalData.windowHeight,
        statusBarHeight:app.globalData.statusBarHeight,
        navHeight:app.globalData.navHeight,
        marginTop:app.globalData.navTitleTop,
        domain:app.globalData.domain,      
      })
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getIndex();
    this.getLocation();
  },
  getLocation(){
    let that=this;
    wx.getLocation({
        success: function(res) {
            console.log(res)
            that.setData({
                now_lat:res.latitude,
                now_lng:res.longitude
              })
              wx.setStorageSync('now_lat', res.latitude)
              wx.setStorageSync('now_lng', res.longitude)
           //that.getDistrict(res.latitude, res.longitude)
           //that.getIndex(res.latitude, res.longitude)
        
        },
        fail(err){
            wx.getSetting({
                 success: (res) => {
                   if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {//非初始化进入该页面,且未授权
                     wx.showModal({
                       title: '是否授权当前位置',
                       content: '需要获取您的地理位置，请确认授权，否则无法获取您所需数据',
                       success: function (res) {
                         if (res.cancel) {
                           wx.showToast({
                             title: '授权失败',
                             icon: 'success',
                             duration: 1000
                           })
                           that.getIndex(0,0)
                         } else if (res.confirm) {
                           wx.openSetting({
                             success: function (dataAu) {
                               if (dataAu.authSetting["scope.userLocation"] == true) {
                                 wx.showToast({
                                   title: '授权成功',
                                   icon: 'success',
                                   duration: 1000
                                 })
                                 //再次授权，调用getLocationt的API
                                 //getLoca(that);
                               } else {
                                 wx.showToast({
                                   title: '授权失败',
                                   icon: 'success',
                                   duration: 1000
                                 })
                               }
                             }
                           })
                         }
                       }
                     })
                   } 
                 }
               })
        }
      });
},
getDistance:function(latitude1,longitude1,latitude2,longitude2){
  let that=this;
  console.log('latitude1:'+latitude1+' longitude1:'+longitude1+' latitude2:'+latitude2+' longitude2:'+longitude2);
  qqmapsdk.calculateDistance({
    mode:'driving',
    from:{
      latitude:latitude1,
      longitude:longitude1
    },
    to:[{
      latitude:latitude2, 
      longitude:longitude2
    }],
    success:function(res,data){
        console.log(data);
        var distance=data.distance[0]/1000;
        that.setData({
          distance:distance
        })
    }
  })
},
call(){
  wx.makePhoneCall({
    phoneNumber: this.data.store.phone,
  })
},
  getIndex:function(){
    var open=wx.getStorageSync('open');
    
   // var member_id=userInfo.member_id;
    let that=this;
    wx.request({
      url:app.globalData.domain + 'wechat/index/getIndex',
      data:{form:'index',open_id:open.id,openid:open.openid},
      success:res=>{
        console.log(res);
        var data=res.data;
        var lat=wx.getStorageSync('now_lat');
        var lng=wx.getStorageSync('now_lng');
              console.log(lat);
              if(lat){
                //console.log(lat);
                that.getDistance(lat,lng,data.store.lat,data.store.lng);
              }
        that.setData({
         store:data.store,
         banner:data.banner,
         wash_order:data.wash_order,
         type:data.type,
         car:data.car,
         weather:data.weather,
         mine:data.mine
          
        })
      }
    })
  }, //swiper滑动事件
  swiperChange:function(e){
   // console.log(e.detail.current);
      this.setData({
          nowIdx: e.detail.current
      })
  },
  fn:function(e){
    congsole.log(e);
  },
  goto:function(e){
    console.log(e);
    var url=e.currentTarget.dataset.url;
    var index=e.currentTarget.dataset.index;
    var parent_id=wx.getStorageSync('parent_id');
    if(url=="#"){

    }else if(url=='phone'){
        wx.makePhoneCall({
          phoneNumber: '400-800-2177',
        })
    }else if(index=='index'){
        wx.reLaunch({
          url: url,
        })
    }else{
        wx.navigateTo({
          url: url,
        })
    }
    console.log(url);
},
  onRefresh: function() {
      this.onLoad()
      //this.onReady()
    },
    gotoNavigate:function(e){
      //console.log(e);
      var store= this.data.store;    
      var latitude=store.lat;
      var longitude=store.lng;       
     var position=store.name;
     var address=store.address;
    
     wx.navigateTo({
       url: '../map/navigate?latitude='+latitude+'&longitude='+longitude+'&position='+position+'&address='+address,
     })
     
    },
    imgLoad(e){
      console.log(e);
      var imgWidth = e.detail.width;
      var imgHeight = e.detail.height;
      var width = this.data.width-20;
      var height = (width/imgWidth)*imgHeight;
      this.setData({
        swiperH:height
      })

    },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})